﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Enum
{
	//枚举思想：就是列出全部的可能性
	public class Program
	{
		static void Main(string[] args)
		{
			Stopwatch watch = new Stopwatch();
			watch.Start();
			EnumMethodOne();
			watch.Stop();
			Console.WriteLine("\n方法1耗费的时间：" + watch.ElapsedMilliseconds + "毫秒");
			watch.Start();
			EnumMethodTwo();
			watch.Stop();
			Console.WriteLine("\n方法2耗费的时间：" + watch.ElapsedMilliseconds+"毫秒");
			Console.ReadLine();
		}

		/*
		 * 问题：
		 * 下面是一个填写数字的模板，其中每个字都代表数字中的”0~9“，那么要求我们输入的数字能够满足此模板
		 *		算		法		洗		脑		题
		 *		*								题
		 *		——————————
		 *		题		题		题		题		题
		 */
		//方法1 时间复杂度O(10^5)  O(n^5)
		public static void EnumMethodOne()
		{
			int count = 0;
			//“算”字的取值范围
             for (int i1 = 1; i1 < 10; i1++)
             {
                 //“法”字的取值范围
                 for (int i2 = 0; i2 < 10; i2++)
                 {
                     //“洗”字的取值范围
                     for (int i3 = 0; i3 < 10; i3++)
                     {
                         //"脑"字的取值范围
                         for (int i4 = 0; i4 < 10; i4++)
                         {
                             //"题"字的取值范围
                             for (int i5 = 1; i5 < 10; i5++)
                             {
                                 count++;
                                 //一个猜想值
                                 var guess = (i1 * 10000 + i2 * 1000 + i3 * 100 + i4 * 10 + i5) * i1;
                                 //最终结果值
                                 var result = i5 * 100000 + i5 * 10000 + i5 * 1000 + i5 * 100 + i5 * 10 + i5;
                                 if (guess == result)
                                 {
                                     Console.WriteLine("\n\n不简单啊，费了我  {0}次,才tmd的找出来\n\n", count);
                                     Console.WriteLine("\t{0}\t{1}\t{2}\t{3}\t{4}", i1, i2, i3, i4, i5);
                                     Console.WriteLine("\n\n\tX\t\t\t\t{0}", i1);
                                     Console.WriteLine("—————————————————————————————");
                                     Console.WriteLine("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", i5, i5, i5, i5, i5, i5);
									 return;
                                 }
                                 Console.WriteLine("第{0}搜索", count);
                             }
                         }
                     }
                 }
             }
		}


		//方法2 时间复杂度O(10^2)  O(n^2)
		public static void EnumMethodTwo()
		{
			//商
             int[] resultArr = { 111111, 222222, 333333, 444444, 555555, 666666, 777777, 888888, 999999 }; 
             //除数
             int[] numArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
             int count = 0;
             for (int i = 0; i < resultArr.Count(); i++)
             {
                 for (int j = 0; j < numArr.Count(); j++)
                 {
                     count++;
                     var result = resultArr[i].ToString();
                     var num = numArr[j].ToString();
                     var origin = (resultArr[i] / numArr[j]).ToString();
                     if (origin.LastOrDefault() == result.FirstOrDefault()&& origin.FirstOrDefault() == num.FirstOrDefault()&& result.Length - 1 == origin.Length)
                     {
                         Console.WriteLine("\n\n费了{0} 次，tmd找出来了", count);
                         Console.WriteLine("\n\n现在的时间复杂度已经降低到O(n2)，相比之前方案已经是秒杀级别\n");
                         Console.WriteLine("\t{0}\t{1}\t{2}\t{3}\t{4}", origin.ElementAt(0), origin.ElementAt(1), origin.ElementAt(2), origin.ElementAt(3), origin.ElementAt(4));
                         Console.WriteLine("\n\n\tX\t\t\t\t{0}", num);
                         Console.WriteLine("—————————————————————————————");
                         Console.WriteLine("\n{0}\t{1}\t{2}\t{3}\t{4}\t{5}", result.ElementAt(0), result.ElementAt(0), result.ElementAt(0), result.ElementAt(0), result.ElementAt(0), result.ElementAt(0));
						 return;
                     }
                     Console.WriteLine("第{0}搜索", count);
                 }
             }
             Console.WriteLine("无解");
		}

	}
}
